package com.yuantu.judiciary.controller;

import com.yuantu.judiciary.common.Response;
import com.yuantu.judiciary.common.ResponseFactory;
import com.yuantu.judiciary.model.form.*;
import com.yuantu.judiciary.model.vo.*;
import com.yuantu.judiciary.service.ICheckPersonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 个人尽调模块
 *
 * @author syw
 * @since 2023-06-24
 **/
@RestController
@RequestMapping(value = "/check-person")
@Api(tags = "个人尽调模块")
@RequiredArgsConstructor
public class CheckPersonController extends BaseController {

    private final ICheckPersonService checkPersonService;


    @ApiOperation(value = "获取查询信息二维码")
    @GetMapping("/get/code")
    public Response<String> getQueryCode(@RequestParam String mark, @RequestParam String mode, @RequestParam Long id) {
        Long userId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(checkPersonService.getQueryCode(userId, mark, mode, id));
    }


    @ApiOperation(value = "二维码跳转查询接口")
    @GetMapping("/get/brief/no/token")
    public Response<CheckPersonBriefVO> getQueryCode(@RequestParam Long id, @RequestParam Long userId) {
        GetQueryInfoForm getQueryInfo = new GetQueryInfoForm();
        getQueryInfo.setId(id);
        return ResponseFactory.okResponse(checkPersonService.getBrief(userId, getQueryInfo));
    }


    @ApiOperation(value = "获取摘要")
    @PostMapping("/get/brief")
    public Response<CheckPersonBriefVO> getBrief(@RequestBody @Validated GetQueryInfoForm form) {
        Long userId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(checkPersonService.getBrief(userId, form));
    }

    @ApiOperation(value = "获取商冲记录")
    @PostMapping("/business")
    public Response<String> searchPersonBusiness(@RequestParam Long id) {
        Long userId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(checkPersonService.searchPersonBusiness(userId, id));
    }

    @ApiOperation(value = "获取商冲记录次数检查")
    @PostMapping("/business-check")
    public Response<PersonBusinessCheckVO> searchPersonBusinessCheck(@RequestParam Long id) {
        Long userId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(checkPersonService.searchPersonBusinessCheck(userId, id));
    }

    @ApiOperation(value = "获取刑事犯罪记录列表")
    @PostMapping("/search/criminal/page")
    public Response<PageVO<CaseCriminalVO>> searchCriminalPage(@PageableDefault Pageable pageable,
                                                               @RequestBody SearchCriminalPageForm form) {
        Long userId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(checkPersonService.searchCriminalPage(userId, form, pageable));
    }

    @ApiOperation(value = "获取诉讼记录列表")
    @PostMapping("/search/litigation/page")
    public Response<PageVO<CaseLitigationVO>> searchLitigationPage(@PageableDefault Pageable pageable,
                                                                   @RequestBody SearchLitigationPageForm form) {
        Long userId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(checkPersonService.searchLitigationPage(userId, form, pageable));
    }

    @ApiOperation(value = "获取我的诉讼记录id")
    @GetMapping("/get/litigation/my")
    public Response<Long> mySearchLitigationPage() {
        Long userId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(checkPersonService.mySearchLitigationPage(userId));
    }

    @ApiOperation(value = "获取案件详情")
    @PostMapping("/get/case-info")
    public Response<Map> getCaseInfo(@RequestBody GetCaseInfoForm form) {
        Long userId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(checkPersonService.getCaseInfo(userId, form));
    }

    @ApiOperation(value = "获取筛选条件-案由")
    @GetMapping("/get/criminal/query-condition/case-reason")
    public Response<List<String>> getCriminalQueryConditionCaseReason(@RequestParam Long id) {
        Long userId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(checkPersonService.getCriminalQueryConditionCaseReason(userId, id));
    }

    @ApiOperation(value = "获取筛选条件-案件类型")
    @GetMapping("/get/criminal/query-condition/case-type")
    public Response<List<String>> getCriminalQueryConditionCaseType(@RequestParam Long id) {
        Long userId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(checkPersonService.getCriminalQueryConditionCaseType(userId, id));
    }

    @ApiOperation(value = "获取限制高消列表")
    @PostMapping("/search/limit-high/page")
    public Response<PageVO<LimitHighDataVO>> searchLimitHighPage(@PageableDefault Pageable pageable,
                                                                 @RequestBody @Validated GetQueryInfoForm form) {
        Long userId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(checkPersonService.searchLimitHighPage(userId, form, pageable));
    }

    @ApiOperation(value = "获取失信列表")
    @PostMapping("/search/dishonest-info/page")
    public Response<PageVO<DishonestInfoDataVO>> searchDishonestInfoPage(@PageableDefault Pageable pageable,
                                                                         @RequestBody @Validated GetQueryInfoForm form) {
        Long userId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(checkPersonService.searchDishonestInfoPage(userId, form, pageable));
    }

    @ApiOperation(value = "获取个人报告")
    @PostMapping("/get/report")
    public Response<CheckPersonReportVO> getReport(@RequestBody GetQueryInfoForm form) {
        Long userId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(checkPersonService.getReport(userId, form));
    }

    @ApiOperation(value = "下载个人报告")
    @GetMapping("/download/report")
    public void downloadReport(@RequestParam Long id, HttpServletResponse response) {
        Long userId = getLoginAccount().getAccountId();
        checkPersonService.downloadReport(response, userId, id);
    }

    @ApiOperation(value = "上传授权文件")
    @PostMapping(value = "/upload/authorized-file")
    // @ApiImplicitParam(name = "file", value = "上传的文件", dataType = "_File", dataTypeClass = MultipartFile.class, required = true)
    public Response<String> uploadAuthorizedFile(@RequestPart(value = "file") MultipartFile file,
                                                 @RequestPart(value = "name") String name,
                                                 @RequestPart(value = "idNumber") String idNumber,
                                                 @RequestPart(value = "phone", required = false) String phone) {
        Long accountId = getLoginAccount().getAccountId();
        SendAuthorizedCodeForm form = new SendAuthorizedCodeForm(name, idNumber, phone);
        return ResponseFactory.okResponse(checkPersonService.uploadAuthorizedFile(accountId, file, form));
    }

    @ApiOperation(value = "添加授权记录")
    @PostMapping("/add/authorized-record")
    public Response<Long> addAuthorizedRecord(@RequestBody @Validated AddPerAuthRecordForm form) {
        Long accountId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(checkPersonService.addAuthorizedRecord(accountId, form));
    }


    @ApiOperation(value = "微信小程序直接生成查询个人授权记录")
    @PostMapping(value = "/person/auth")
    public Response<Long> personAuthQueryRecord(@RequestParam Long accountId,
                                                @RequestParam String name,
                                                @RequestParam String idNumber,
                                                @RequestParam String phone) {
        SendAuthorizedCodeForm form = new SendAuthorizedCodeForm(name, idNumber, phone);
        return ResponseFactory.okResponse(checkPersonService.personAuthQueryRecord(accountId, form));
    }


}
